<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form
            inline
            size="small"
            :model="from"
            ref="from"
            :label-position="labelPosition"
            label-width="100px"
          >
            <el-row>
              <el-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18">
                <el-form-item label="数据场：">
                  <el-input
                    v-model="from.dataFactoryName"
                    placeholder="请输入数据场"
                    clearable
                    class="selWidth"
                  />
                </el-form-item>
                <el-form-item label="账户/用户名：">
                  <el-input
                    v-model="from.userName"
                    placeholder="请输入账户/用户名"
                    clearable
                    class="selWidth"
                  />
                </el-form-item>
                <el-form-item label="币种：">
                  <el-select
                    v-model="from.currencyId"
                    placeholder="请选择币种"
                    clearable
                    class="selWidth"
                  >
                    <el-option
                      v-for="item in options"
                      :key="item.id"
                      :label="item.currencyName"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="开始时间：">
                  <el-date-picker
                    v-model="from.beginTime"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择日期"
                    clearable
                    class="selWidth"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间：">
                  <el-date-picker
                    v-model="from.endTime"
                    type="datetime"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择日期"
                    clearable
                    class="selWidth"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col
                :xs="24"
                :sm="24"
                :md="24"
                :lg="6"
                :xl="6"
                class="text-right from"
              >
                <el-form-item>
                  <el-button
                    class="mr10"
                    size="small"
                    type="primary"
                    icon="el-icon-search"
                    @click="search()"
                    >搜索</el-button
                  >
                  <el-button class="mr10" icon="el-icon-refresh" size="small" @click="reset()"
                    >重置</el-button
                  >
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div class="btn_bt">
          <el-button class="mr10" icon="el-icon-top" size="small" @click="exportTable()"
            >导出表格</el-button
          >
        </div>
      </div>
      <el-table border
        ref="table"
        v-loading="loading"
        :data="tableData.data"
        style="width: 100%"
        size="mini"
        highlight-current-row
      >
        <el-table-column
          prop="dataFactoryName"
          label="数据场"
          min-width="100"
        />
        <el-table-column prop="userName" label="账户/用户名" min-width="150" />
        <el-table-column prop="initEquity" label="初始权益" min-width="100" />
        <!-- <el-table-column prop="endEquity" label="期末权益" min-width="100" /> -->
        <el-table-column prop="endEquity" label="期末权益" min-width="100">
          <template slot-scope="scope">
            <span>{{
              parseFloat(`${scope.row.endEquity}`)
                .toFixed(2)
                .replace(/\B(?=(\d{3})+(?!\d))/g, ",")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="closeoutProfit" label="平仓结果" min-width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.closeoutProfit == 0">0</span>
            <span v-if="scope.row.closeoutProfit < 0" class="g">{{
              parseFloat(`${scope.row.closeoutProfit}`)
                .toFixed(2)
                .replace(/\B(?=(\d{3})+(?!\d))/g, ",")
            }}</span>
            <span v-if="scope.row.closeoutProfit > 0" class="r">{{
               parseFloat(`${scope.row.closeoutProfit}`)
                .toFixed(2)
                .replace(/\B(?=(\d{3})+(?!\d))/g, ",")
            }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="totalFee" label="服务费" min-width="100" /> -->

        <el-table-column prop="totalFee" label="服务费" min-width="100">
          <template slot-scope="scope">
            <span>{{
              parseFloat(`${scope.row.totalFee}`)
                .toFixed(2)
                .replace(/\B(?=(\d{3})+(?!\d))/g, ",")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="amountChange" label="额度变更" min-width="100">
          <template slot-scope="scope">
            <span>{{
              parseFloat(`${scope.row.amountChange}`)
                .toFixed(2)
                .replace(/\B(?=(\d{3})+(?!\d))/g, ",")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="beginTime" label="开始时间" min-width="150" />
        <el-table-column prop="endTime" label="结束时间" min-width="150" />
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[15, 30, 45, 60]"
          :page-size="from.limit"
          :current-page="from.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { currencyGetOption } from "@/api/transaction";
import {
  reportGetUserCapital,
  reportUserCapitalExport,
} from "@/api/reportQuery";
export default {
  data() {
    return {
      options: [],
      labelPosition: "right",
      from: {
        dataFactoryName: "",
        userName: "",
        currencyId: "",
        beginTime: "",
        endTime: "",
        page: 1,
        limit: 15,
      },
      loading: false,
      tableData: {
        data: [],
        total: 0,
      },
    };
  },
  mounted() {
    currencyGetOption()
      .then((res) => {
        console.log("币种全列表", res);
        this.options = res;
        this.getlist();
      })
      .catch((err) => {
        console.log(err);
      });
  },
  methods: {
    exportTable() {
      reportUserCapitalExport(this.from)
        .then((res) => {
          console.log("导出表格", res);
          window.open(res.filename);
        })
        .catch((err) => {
          console.log(err);
          this.$message.error(err);
        });
    },
    search() {
      this.from.page = 1;
      this.from.limit = 15;
      this.getlist();
    },
    reset() {
      this.from = {
        dataFactoryName: "",
        userName: "",
        currencyId: "",
        beginTime: "",
        endTime: "",
        page: 1,
        limit: 15,
      };
      this.getlist();
    },
    getlist() {
      this.loading = true;
      reportGetUserCapital(this.from)
        .then((res) => {
          console.log(res);
          this.tableData.data = res.list;
          this.tableData.total = res.total;
          this.loading = false;
        })
        .catch((err) => {
          console.log(err);
          this.loading = false;
        });
    },
    pageChange(page) {
      this.from.page = page;
      this.getlist();
    },
    handleSizeChange(val) {
      this.from.limit = val;
      this.getlist();
    },
  },
};
</script>

<style lang="scss" scoped>
.btn_bt {
  border-top: 1px dashed #ccc;
  padding-top: 20px;
}

.r {
  color: #ff210d;
}

.g {
  color: #019101;
}
</style>
